<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import = "java.util.*,support.*, java.sql.*" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Provide degrees - Choose Location</title>
</head>
<body>
	<%
		
		Student student = (Student)session.getAttribute("student");
		ArrayList<Degree> degreeList = (ArrayList<Degree>)session.getAttribute("degreelist");
		if (request.getParameter("address") != null)
			student.address(request.getParameter("address"));
		if (request.getParameter("city") != null)
			student.city(request.getParameter("city"));
		if (request.getParameter("state") != null)
			student.state(request.getParameter("state"));
		if (request.getParameter("zipcode") != null)
			student.zipcode(request.getParameter("zipcode"));
		Degree degree = new Degree();
		session.setAttribute("degree", degree);
	%>
	
	
	<h3>Previously entered in information:</h3>

	<p><%= "Full name: " + student.getFirstName() + " " + student.getMiddleName() + " " + student.getLastName() %></br>
	<%= "Country of Citizenship: " + student.getCitizenshipName() %></br>
	<% if (student.getState() != "") { %>
		<%= "Address: " + student.getAddress() + ", " + student.getCity() + " " + student.getState() + ", " + student.getZipcode() %>
	<% } else {%>
		<%= "Address: " + student.getAddress() + ", " + student.getCity() + ", " + student.getZipcode() %></p>
	<% } %>
	
	<%
	
   //Print out degree information if coming from more degrees
   if (degreeList != null) {
      for (int i = 0; i < degreeList.size(); i++) {
%>
         <p><b><%= "Degree #" + (i+1) + ": " %></b><br>
         <%= degreeList.get(i).getDisciplineName() + ", " + degreeList.get(i).getTitle() %><br> 
         <%= degreeList.get(i).getUniversityName() %><br>
         <%= degreeList.get(i).getLocationName() %><br>
         <%= "Year of Graduation: " + degreeList.get(i).getDate() %><br>
         <%= "GPA: " + degreeList.get(i).getGpa() %><br>
<%
	   }	   
   }
%>	
  	
  	<h3>Please select the location of your degree:</h3>	

<table>
<% 
	Connection conn = null;
	PreparedStatement stm = null;
	ResultSet result = null;

	try {
	Class.forName("org.postgresql.Driver");
	conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Graduate_application_database", "postgres", "postgres");
	conn.setAutoCommit(false);
	stm = conn.prepareStatement("SELECT * FROM countries_and_states");
	result = stm.executeQuery();
	conn.commit();
	conn.setAutoCommit(true);
   
   for (int i = 0; result.next(); i++)
   {

	  // Begin row every 3 elements.
      if (i%3 == 0)
	  {
%>
         <tr>
<%    }   
%>    <!--  Notice this! -->
      <td><a href=university.jsp?degreeLocation=<%=result.getInt("cs_id")%>><%=result.getString("country_state") %></a></td>

<%    // End row after 3rd element has been printed.
      if ((i+1)%3 == 0)
      {   
%>
         </tr>
<%    }

   }
%>
</table>


<% 
		result.close();
		stm.close();
		conn.close();
	}
	catch (SQLException e)
	{
		throw new RuntimeException(e);
	}
    finally {
        if (result != null) {
            try {
                result.close();
            } 
            catch (SQLException e) { }
            result = null;
        }
        if (stm != null) {
            try {
                stm.close();
            } 
            catch (SQLException e) { }
            stm = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } 
            catch (SQLException e) { }
            conn = null;
        }
    }
%>	


</body>
</html>
